require 'fileutils'
include FileUtils

class AbstractProduct < ActiveRecord::Base
  has_many :products

  GEOMETRY_IMAGE_SIZE = "360"
  MEDIUM_IMAGE_SIZE = "250"
  THUMB_IMAGE_SIZE = "100"

  file_column :image_url, :magick => {:geometry=>GEOMETRY_IMAGE_SIZE,
    :versions => { "thumb" => THUMB_IMAGE_SIZE,"medium" => MEDIUM_IMAGE_SIZE}
  }

  def self.cp_from_cate
    app_root = File.expand_path(RAILS_ROOT)
    count = 0
    Category.find(:all).each{|c|
      if c.products.length > 0 && c.image_url
        ap = self.create(:image_url=>File.basename(c.image_url),:description=>c.description)
        if Product.update_all({:abstract_product_id=>ap.id},['category_id = ?',c.id])
          ap_path = app_root + "/public/abstract_product/image_url/#{ap.id}/"
          Dir.mkdir(ap_path,0775)
          `cp -r #{File.dirname(c.image_url)+"/*"} #{ap_path}`
          count +=1
        end
      end
    }
    return count
  end
end
